

const ArticleColumn = require('../model/article_column.model')
const { Op, Sequelize } = require("sequelize");
const moment = require('moment');
class ArticleColumnService {
  
    // get
    async getArticleColumnService(param) {
        const {column,user_id, createdAtFrom, createdAtTo, examine, examine_user, is_delete, currentPage, pageSize,examine_date_from,examine_date_to} = param
        const where = {};
        column && (where['column'] = {
            [Op.eq]: column
        })   
        user_id && (where['user_id'] = {
            [Op.eq]: user_id
        })
        createdAtFrom && (where['createdAt'] = {
            [Op.between]: [createdAtFrom, createdAtTo]
        })
        examine_date_from && (where['examine_date'] = {
            [Op.between]: [examine_date_from, examine_date_to]
        })
        examine && (where['examine'] = {
            [Op.eq]: examine
        })
        examine_user && (where['examine_user'] = {
            [Op.eq]: examine_user
        })
        is_delete && (where['is_delete'] = {
            [Op.eq]: is_delete
        })
       
        const { count, rows } = await ArticleColumn.findAndCountAll({
            where,
            offset: (currentPage - 1) * pageSize,
            limit: Number(pageSize),
        });
        console.log(rows)
        return rows ? { data: rows, total: count } : { error: 500 }

    }
    // 
    async updateArticleColumnService(param){
       const { column, user_id , examine , examine_user ,result }  = param
       let obj = {}
       examine && (obj['examine'] = examine)
       examine_user && (obj['examine_user'] = examine_user)
       result && (obj['result'] = result)
       obj['examine_date'] = moment(new Date()).format('YYYY-MM-DD hh:mm:ss')
       console.log(obj)
       const res = await ArticleColumn.update(obj, {
           where: {
               user_id: user_id,
               column:column
           }
       })
       return res[0] > 0 ? {status:'success',code:1000} : {status:'fail',code:1001}
    }
}
// 导出后 在控制器controller中使用
module.exports = new ArticleColumnService()